package yja.gzw.app.fragmentview

import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener
import yja.gzw.app.R
import yja.gzw.app.adapter.Fabudingdan2Adapter
import yja.gzw.app.adapter.Fabudingdan2Adapter2
import yja.gzw.app.base.BaseFragment
import yja.gzw.app.databinding.FragmentDindanyiBinding
import yja.gzw.app.viewmodel.Mainviewmodel
import yja.gzw.app.model.UserData
import yja.gzw.app.model.bean.OrderDatafa2


class DindansanFragment :BaseFragment<Mainviewmodel, FragmentDindanyiBinding>(R.layout.fragment_dindanyi){
    var yeshu=1
    var shuju= mutableListOf<OrderDatafa2>()
    private lateinit var fabiada2: Fabudingdan2Adapter2
    override fun initView() {
        viewmodel.onfabu2shwowang2(UserData.token, mapOf("keyword" to "","status" to "2","page" to "1","pagesize" to "10"))
        binding.refreshLayout.setRefreshHeader(ClassicsHeader(context));
        binding.refreshLayout.setRefreshFooter(ClassicsFooter(context));
        binding.refreshLayout.setOnRefreshListener(object : OnRefreshListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                shuju.clear()
                yeshu=1
                viewmodel.onfabu2shwowang2(UserData.token, mapOf("keyword" to "","status" to "2","page" to "1","pagesize" to "10"))

            }


        })

        binding.refreshLayout.setOnLoadMoreListener(object : OnLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {

                if(yeshu==((shuju.size/10)+1)){

                }else{
                    yeshu++
                }

                viewmodel.onfabu2shwowang2(UserData.token, mapOf("keyword" to "","status" to "2","page" to yeshu.toString(),"pagesize" to "10"))


            }


        })


    }

    override fun dataObservese() {
        viewmodel.onfabu2shwo2.observe(this, Observer {
            if(it.code==1){

                if(it.data.data.size<10&&yeshu==1){

                    binding.recyclerView.layoutManager=
                        LinearLayoutManager(context)
                    fabiada2= Fabudingdan2Adapter2(requireContext(),it.data.data,this)
                    binding.recyclerView.adapter= fabiada2
                    binding.refreshLayout.finishRefresh();
                    binding.refreshLayout.finishLoadMore()

                }else{
                    shuju.addAll(it.data.data)
                    binding.recyclerView.layoutManager=
                        LinearLayoutManager(context)
                    fabiada2= Fabudingdan2Adapter2(requireContext(),shuju,this)
                    binding.recyclerView.adapter= fabiada2
                    binding.refreshLayout.finishRefresh();
                    binding.refreshLayout.finishLoadMore()
                    if(yeshu==1){
                        binding.recyclerView.scrollToPosition(0)

                    }else{
                        val shshbu=10*(yeshu-1)
                        binding.recyclerView.scrollToPosition(shshbu-2)

                    }
                }




            }else{

                Toast.makeText(context,it.msg, Toast.LENGTH_SHORT).show()
            }


        })
    }
}